let callbacks = [];
let pending = false;

function flushCallbacks() {
    pending = false;
    for(let i = 0; i < callbacks.length; i++) {
        callbacks[i](); 
    }
}

let timerFunc; // 定义异步方法，优雅降级的方式去调用
if(typeof Promise !== 'undefined') {
    const p = Promise.resolve();
    timerFunc = () => {
        p.then(flushCallbacks);
    }
} else if(typeof MutationObserver !== 'undefined') {
    let counter = 1;
    const observer = new MutationObserver(flushCallbacks);
    const textNode = document.createTextNode(String(counter));
    observer.observe(textNode, {
        characterData: true
    })
    timerFunc = () => {
        counter = (counter + 1) % 2;
        textNode.data = String(counter);
    }
} else if(typeof setImmediate !== 'undefined') {
    timerFunc = () => {
        setImmediate(flushCallbacks);
    }
} else {
    timerFunc = () => {
        setTimeout(flushCallbacks, 0);
    }
}

export function nextTick(cb) {
    callbacks.push(cb);
    if(!pending) {
    // 如果多次调用 nextTick，只会执行一次异步，等异步队列清空之后再把标志变为 false
        pending = true;
        timerFunc();
    }
}